package yyee.llee.mmee.weather.base

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType


/**
 * Created by Lingbaeryi
 */
abstract class BaseWeatherActivity<T : ViewBinding> : AppCompatActivity(), View.OnClickListener {

    val tag: String? = javaClass.simpleName
    lateinit var viewBinding: T
    var isIntentSetting = false


    override fun onCreate(savedInstanceState: Bundle?) {
        initTheme()
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
        val type: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
        val cls = type.actualTypeArguments[0] as Class<*>
        try {
            val inflate = cls.getDeclaredMethod("inflate", LayoutInflater::class.java)
            viewBinding = inflate.invoke(null, layoutInflater) as T
            setContentView(viewBinding.root)
        } catch (e: NoSuchMethodException) {
            e.printStackTrace()
        }
//        //状态栏颜色字体(白底黑字)修改 Android6.0+
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            val window = window
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
//            window.statusBarColor = resources.getColor(R.color.colorWhite, null)
//            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
//        }
        initView()
        initData()
        initListener()
        initNotification()
    }


    open fun initNotification() {

    }


    override fun onRestart() {
        super.onRestart()

    }




    /**
     * 沉浸式状态栏
     */
    protected open fun immersionStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window: Window = window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            // 沉浸式状态栏
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            // 状态栏改为透明
            window.statusBarColor = Color.TRANSPARENT
        }
    }

    /**
     * 主题及沉浸式
     */
    open fun initTheme() {

    }

    /**
     * 控件初始化
     */
    open fun initView() {

    }

    /**
     * 加载数据方法
     */
    open fun initData() {

    }

    /**
     * 点击事件监听
     */
    open fun initListener() {

    }

    /**
     * 点击事件
     */
    override fun onClick(v: View?) {

    }
}